//
//  JZLoginViewController.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/11/17.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "JZLoginViewController.h"
#import "JZRegistViewController.h"
#import "JZChangePassWordViewController.h"
#import "JZGetBackPasswordViewController.h"
#import "JZMenuViewController.h"

@interface JZLoginViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;//头像
@property (weak, nonatomic) IBOutlet UITextField *userName;//账号
@property (weak, nonatomic) IBOutlet UITextField *passWord;//密码

@end

@implementation JZLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //改变声明周期
    [self.headImageView layoutIfNeeded];
    
    //设置头像为圆形
    [self.headImageView.layer setCornerRadius:self.headImageView.frame.size.width/2.0];
    
    [AVUser logOut];
    // Do any additional setup after loading the view.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.userName resignFirstResponder];
    
    [self.passWord resignFirstResponder];
}

//登录
- (IBAction)loginToApp:(id)sender {
    
    
    
    [AVUser logInWithUsernameInBackground:self.userName.text password:self.passWord.text block:^(AVUser *user, NSError *error) {
        
        if (user != nil && !error) {
            //登录成功跳转到登录界面
            [self dismissViewControllerAnimated:YES completion:nil];
                        
            NSLog(@"-------登录成功");
            
        } else {
            
            [AVUser logInWithMobilePhoneNumberInBackground:self.userName.text password:self.passWord.text block:^(AVUser *user, NSError *error) {
                
                if (user != nil && !error) {
                    
                    //登录成功跳转到登录界面
                    [self dismissViewControllerAnimated:YES completion:nil];
                    NSLog(@"登录成功");
                    
                }else{
                   
                    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"账号或密码错误，请重新输入" preferredStyle:(UIAlertControllerStyleAlert)];
                    
                    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"重新输入" style:(UIAlertActionStyleDefault) handler:nil];
                    
                    [alertController addAction:alertAction];
                    [self presentViewController:alertController animated:YES completion:nil];
                    //NSLog(@"账号密码错误，登录失败");
                    
                }
            }];
        }
    }];

    
}

//更多按钮方法实现
- (IBAction)moreChangeAction:(id)sender {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"更多功能" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"更改密码" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        JZChangePassWordViewController *changePasswordVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZChangePassWordViewController"];
        
        [self presentViewController:changePasswordVC animated:YES completion:nil];
        
    }];
    
    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"忘记密码" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        JZGetBackPasswordViewController * getBackPassWordVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZGetBackPasswordViewController"];
        
        [self presentViewController:getBackPassWordVC animated:YES completion:nil];
        
    }];
    
    [alertController addAction:alertAction];
    [alertController addAction:alertAction2];
    [self presentViewController:alertController animated:YES completion:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
